#!/bin/sh
#
# This script fake scrapes the internode
# unmetered radio site, downloads all pls files
# and renames to the station name.
#

if [ -n "$1" ]; then
 EXT="$1"
else
 EXT="pls"
fi

cd /usr/media-library/audio/radio/
rm index.html index.wget *.${EXT}
wget http://www.internode.on.net/residential/entertainment/broadband_radio/
grep -o "http://[a-z./0-9]*${EXT}" index.html > index.wget
wget -i index.wget
rm playlist.${EXT}

for PLAYLIST in *.${EXT}
do

 case ${EXT} in
	pls)
         # Ready titles for renaming files
         TITLE=`grep "Title1*" "${PLAYLIST}" | sed s/Title1\=//`
         TITLE=`echo ${TITLE} | sed s/\\:/\ -/`
	 ;;
	m3u)
	 # Ready titles for renaming files
	 TITLE=`grep "#EXTINF*" "${PLAYLIST}" | sed s/\#EXTINF\:\-1\,//`
	 TITLE=`echo ${TITLE} | sed s/\\:/\ -/`
	 ;;
	strm)
         # Ready titles for renaming files
         TITLE=`grep "#EXTINF*" "${PLAYLIST}" | sed s/\#EXTINF\:\-1\,//`
         TITLE=`echo ${TITLE} | sed s/\\:/\ -/`
         # Get Direct Link
         DIRECT=`grep "://" "${PLAYLIST}"`
	 ;;
	*)
	echo "You shouldn't be here"
	exit 1
 esac

# Generate Bookmarks list for Streamer on iPhone
	URL=`grep "File1*" "${PLAYLIST}" | sed s/File1\=//`
	echo "# 1 ${TITLE}" >> playlist1.tmp
	echo "${URL}" >> playlist1.tmp

# Give the stream a useful filename 
        echo ${TITLE}
        mv "${PLAYLIST}" "${TITLE}.${EXT}"

done

mv playlist1.tmp playlist1.${EXT}

# Upload to IPHONES
# uploadtoiphonestreamer
rm playlist1.pls

rm index*
rm *.${EXT}.1
